(function() {
  var ctrls = angular.module(MyAppConfig.controllers);
  ctrls.controller('UserManageCtrl', ['$scope', '$log', 'DialogService', 'DataService', UserManageCtrl]);

  function UserManageCtrl($scope, $log, DialogService, DataService) {
    $log.debug('UserManageCtrl init...');

    // 处理scope销毁
    $scope.$on('$destroy', function() {
      $log.debug('UserManageCtrl destroy...');
    });

    $scope.page = {
      pageNumber: 1,
      pageSize: 5
    };

    $scope.query = function() {
      DialogService.showWait('数据查询中。。。');
      DataService.send(
        '/tbadminuser/query',
        {
          page: $scope.page
        },
        function(data) {
          DialogService.hideWait();
          if (data.success) {
            $scope.list = data.datas.list;
            $scope.page = data.datas.page;
            return;
          }
          DialogService.showAlert(data.message);
        }
      );
    };
    $scope.query();

    $scope.toPage = function(pn) {
      if (pn <= 0 || pn > $scope.page.pageCount || pn == $scope.page.pageNumber) {
        return;
      }
      //分页查询
      $scope.page.pageNumber = pn;
      $scope.query();
    };

    //禁用
    $scope.toDelete = function(user) {
      DialogService.showConfirm('是否禁用：' + user.nickname + '?', function() {
        DialogService.showWait('禁用中请稍后');
        DataService.send(
          '/tbadminuser/admindelete',
          {
            tbUser: user
          },
          function(data) {
            DialogService.hideWait();
            DialogService.showAlert(data.message, function() {
              if (data.success) {
                $scope.query();
              }
            });
          }
        );
      });
    };

    //启用
    $scope.toUnDelete = function(user) {
      DialogService.showConfirm('是否启用：' + user.nickname + '?', function() {
        DialogService.showWait('启用中请稍后');
        DataService.send(
          '/tbadminuser/nuadmindelete',
          {
            tbUser: user
          },
          function(data) {
            DialogService.hideWait();
            DialogService.showAlert(data.message, function() {
              if (data.success) {
                $scope.query();
              }
            });
          }
        );
      });
    };

    //修改
    $scope.toUpdate = function(type) {
      DialogService.setTempDialogTitle('修改类型');
      DialogService.showCustom('templates/manage/usermanage-update.html', type, function() {
        $scope.query();
      });
    };
  }
})();
